function[eef_response] = calc_eef_response_tensor(fday)

% The function calculates the EEF response from ACE IEF Ex and Ez
% data through the tensor relationship. 
% The native resolution is 5 minutes. I. e. , the OMNI ace data
% are available at 5 min interval. If the time stamp of fday
% is different, then the filter outout is interpolated to match the
% input time stamps..
% fday Decimal days in MATLAB format : A serial date number represents the whole and fractional 
% number of days from a specific date and time, where datenum('Jan-1-0000 00:00:00') 
% returns the number 1. (The year 0000 is merely a reference point and is not intended 
% to be interpreted as a real year in time.)

%Load filter coefficients

load /Users/manojnair/Documents/projects/ace_tensor/fliter_coefficients_tx_ty a b a_n b_n;

%Load ACE data

if ~exist('ace_all','var'),
    
    if ispc == 1,
        
        load E:\projects\ace_tensor\acedata\ace_2000_2010.mat ace_all;
        ace_all(:,1) = ace_all(:,1)+(17)/(60*24);%Try to advance 60 VS model/ ace time minutes
        
        
    end;
    if isunix == 1,
        load /Users/manojnair/Documents/projects/ace_tensor/acedata/ace_2000_2010.mat ace_all;
        ace_all(:,1) = ace_all(:,1)+(17)/(60*24);%Try to advance 60 VS model/ ace time minutes
        
    end;
        
end;


% check whether fday is within the ace data availability period

if fday(1) < ace_all(1,1) || fday(1) > ace_all(end,1) ...
        || fday(end) < ace_all(1,1) || fday(end) > ace_all(end,1),
    fprintf ('Error, time out of bounds\n');
    eef_response = 0;
   
    
    return;
end;

    L = ace_all(:,1) >= fday(1) - (20 / (24 * 60)) & ace_all(:,1) <= fday(end) + (20 / (24 * 60));
    
    % Check whether there are enough data
    
    % OK don't do this at this time
    
    
    
    if sum(L) > 10,
    
    ace_time = ace_all(L,1);
    ace_ey = ace_all(L,2);
    ace_ez = ace_all(L,3);
    
    L = isnan(ace_ey);
    
    if sum(L)/length(L) < 0.1 & sum(L) ~= 0,
        ace_ey = interp1(ace_time(~L),ace_ey(~L),ace_time);
      
    
    elseif sum(L)/length(L) > 0.1,
       fprintf('Too many missing IEF data points\n');
       eef_response = 0;
       return;

    end;

    L = isnan(ace_ez);
    
    if sum(L)/length(L) < 0.1 & sum(L) ~= 0,
       
        ace_ez = interp1(ace_time(~L),ace_ez(~L),ace_time);
    
    elseif sum(L)/length(L) > 0.1,
       fprintf('Too many missing IEF data points\n');
       eef_response = 0;
       return;

    end;

    if ~any(isnan(ace_ey)) & ~any(isnan(ace_ez)),
        
        ace_ey = interp1(ace_time, ace_ey, fday);
        ace_ez = interp1(ace_time, ace_ez, fday);
        
        eef_response = filter(b_n,a_n,ace_ey) + filter(b,a,ace_ez); 
    
    else
       fprintf('Something went wrong during interpolation\n');
       eef_response = 0;
       return;
    end;
  
   
   else
       fprintf('Not sufficient ACE data for the period\n');
       eef_response = 0;
       return;
    end;
   


